package ltd.hyangyang.vegetable.utils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 分页工具类
 * 根据相关的请求中传递的参数获取分页信息
 */
public class PageUtils {

    private static String CURRENT_PAGE = "pageNum";
    private static String PAGE_SIZE = "pageSize";

    private static PageParam pageParam;

    public static <T> Page<T> getPage(){
        PageParam pageParam = getPageParams();
        Page<T> page = new Page<>(pageParam.getPageNum(),pageParam.getPageSize());
        return page;
    }

    public static PageParam getPageParams() {
        //获取相关的信息
        pageParam = new PageParam();
        //自定义参数转换器
        pageParam.setPageNum(
                ConverterUtils.toInt(ServerUtils.getParamBykey(CURRENT_PAGE))==null?
                        pageParam.getPageNum():
                        ConverterUtils.toInt(ServerUtils.getParamBykey(CURRENT_PAGE)));
        pageParam.setPageSize(
                ConverterUtils.toInt(ServerUtils.getParamBykey(PAGE_SIZE))==null?
                        pageParam.getPageSize() :
                        ConverterUtils.toInt(ServerUtils.getParamBykey(PAGE_SIZE)));

        return pageParam;
    }
}
